package com.inspur.servlet;

import com.inspur.bean.Category;
import com.inspur.bean.Product;
import com.inspur.service.impl.CategoryServiceImpl;
import com.inspur.service.impl.ProductServiceImpl;
import com.inspur.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author：齐文勇
 * @Package：${PACKAGE_NAME}
 * @Project：ProductMS
 * @name：${NAME}
 * @Date：2024/10/23 14:23
 * @Filename：${NAME}
 */
@WebServlet(urlPatterns = "/ProductListServlet")
public class ProductListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
//        //调用service获取列表
//        List<Product> productList = new ProductServiceImpl().findAll();
//        //将集合放入request
//        request.setAttribute("productList",productList);
//        //通过请求转发到列表界面
//        request.getRequestDispatcher("pages/product/list.jsp").forward(request,response);

        //接收当前第几页
//        int pageNum = 1;
        String pn = request.getParameter("pageNum");
        int pageNum = pn != null && !"".equals(pn) ?  Integer.parseInt(pn) : 1;
        int pageSize = 4;
        //接收页面条件
        String name = request.getParameter("name");
        String state = request.getParameter("state");
        String category = request.getParameter("category");

        //将条件封装到bean
        Product product = new Product();
        product.setState(state!=null ?Integer.parseInt(state):2);
        product.setCategory(category);
        product.setName(name);
        //调用service获取分页数据
        PageUtil<Product> pageUtil = new ProductServiceImpl().findProductPage(product, pageSize, pageNum);
        //商品类型的列表

        List<Category> categoryList = new CategoryServiceImpl().findAll();

        //放入request范围
        request.setAttribute("pageUtil",pageUtil);
        request.setAttribute("categoryList",categoryList);
        request.getRequestDispatcher("pages/product/list.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
